/**
 * Created by Administrator on 2017/2/24 0024.
 */

var pageNo = 1;     //当前页
var pageSize = 5;   //每页记录数
var pageCount;
var tabCurrent = 0; //当前标签，进校，出校，异常记录
var dataJson;       //查询参数
$(function () {
    $(".search-btn").click(function () {
        pageNo = 1;
        queryData();
        return false;
    });
    $("#date-start").focusout(function () {
        pageNo = 1;
        queryData();
    });
    $("#date-end").focusout(function () {
        pageNo = 1;
        queryData();
    });
    queryData();
});

function queryData() {
    tabCurrent = $("#type").attr("value");
    var sdate = $("#date-start").val();
    var edate = $("#date-end").val();
    var stuName = $(".seek-box").val();

    if (sdate == "") {
        sdate = "1970-01-01 00:00:00";
    }
    if (edate == "") {
        edate = "2160-01-01 00:00:00";
    }
    var startDate = new Date(sdate.replace("T"," "));
    var endDate = new Date(edate.replace("T"," "));
    dataJson = {
        startDate: DateUtils.format(startDate, "yyyy-MM-dd HH:mm:ss"),
        endDate: DateUtils.format(endDate, "yyyy-MM-dd HH:mm:ss"),
        stuName: stuName,
        pageNo : pageNo,
        pageSize : pageSize,
        type: tabCurrent
    };
    getDataOfRecord();
}

function getDataOfRecord() {//带参分页查询函数
    $.ajax({
        async:false,
        type:'POST',
        url:'/door/front/teacher/record',
        data:dataJson,
        traditional:true,
        cache:false,
        dataType : 'json',
        contentType:'application/x-www-form-urlencoded',
        beforeSend:function (xhr) { },
        success:function (data,textStatus,xhr) {
            if(check(data)){
                if(data.itemCount==0){
                    layer.msg("没有数据",{time:1000});
                }
                pageCount=Math.floor((data.itemCount + pageSize - 1) / pageSize);
                setTableBTN();
                setDataOfList(data.list);
            }
        },
        error:function (xhr,textStatus,errorInfo) { },
        complete:function (xhr,textStatus) {}
    });
}


function setDataOfList(str) {
    var table = $("table");
    table.empty();
    for (var i = 0; i < str.length; i++) {
        var type = $("#type").attr("value");
        if (type != 3) {
            table.append("<tr><td>" + str[i].studentName + "</td><td>" + DateUtils.format(new Date(str[i].createTime), 'yyyy-MM-dd HH:mm:ss') + "</td></tr>");
        } else {
            table.append("<tr><td>" + str[i].studentName + "</td><td>" + (str[i].inoutType == 1 ? '出校' : '进校') + "</td><td>"
                + DateUtils.format(new Date(str[i].createTime), 'yyyy-MM-dd HH:mm:ss') + "</td></tr>");
        }
    }
}

function setTableBTN() {
    $("#page").unbind();

    $("#page option").remove();

    for(var i =1;i<=pageCount;i++){
        if(i!=pageNo)
            $("#page").append("<option>"+i+"</option>");
        else
            $("#page").append("<option selected=true>"+i+"</option>");
    }

    $("#page").change(function (i) {
        console.log($("#page").find("option:selected").text());
        pageNo = parseInt($("#page").find("option:selected").text());
        queryData();
    });

    $(".page-nav span").each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
                if (pageNo > 1) {
                    pageNo--;
                    queryData();
                } else {
                    layer.msg("当前是第一页",{time:1000});
                }
            };
        } else if (i == 2) {//下一页按钮
            this.onclick = function () {
                if (pageNo < pageCount) {
                    pageNo++;
                    queryData();
                } else {
                    layer.msg("当前是最后一页",{time:1000});
                }
            };
        } else if (i == 0) {
            this.onclick = function () {
                pageNo = 1;
                queryData();
            };
        } else if (i == 3) {
            this.onclick = function () {
                pageNo = pageCount;
                queryData();
            };
        }
    });
}